home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9327 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.3 KB

  1. Path: hubcap.clemson.edu!hubcap!mjs
  2. From: mjs@hubcap.clemson.edu (M. J. Saltzman)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: NEwbie: How to return a multi-dimensional array from function?
  5. Date: 8 Mar 96 15:23:49 GMT
  6. Organization: Clemson University
  7. Message-ID: <mjs.826298629@hubcap>
  8. References: <4hp273$8bu@news.xs4all.nl> <31404CE9.1A4A@mc.net>
  9. NNTP-Posting-Host: hubcap.clemson.edu
  10. X-Newsreader: NN version 6.5.0 #1
  11.  
  12. Sean Park <spark@mc.net> writes:
  13.  
  14. >Anthony Moendir wrote:
  15. >> 
  16. >> I want to return a multidimensional array fro a function,
  17. >> something like this:
  18. >> 
  19. >> char *Foo();
  20. >> 
  21. >> int main()
  22. >> {
  23. >>  char tmp[10][5];
  24. >> 
  25. >>  tmp=Foo();
  26. >> }
  27. >> 
  28. >> char *Foo()
  29. >> {
  30. >>  char tmp[10][5];
  31.  
  32. Two things: (1) If you are going to use the value of tmp outside of Foo(),
  33. then you need to declare
  34.  
  35.     static tmp[10][5];
  36.  
  37. Otherwise the storage may disappear when you return from Foo().
  38.  
  39. >> //do something
  40. >> return(tmp);
  41.          ^   ^ Parentheses not required here.
  42. >> }
  43.  
  44. (2) When you use tmp in an expression, the compiler replaces the array
  45. name with a constant pointer to its first element.  (Among other
  46. consequences, this means that arrays can't appear on the left side of
  47. assignments.)  In this case, the first element has type
  48. array-of-15-chars, so a pointer to it is a
  49. pointer-to-array-of-15-chars.  That's the type that the function
  50. should return, and the type of the variable that you assign it to.
  51. Thus, Foo() needs to be declared
  52.  
  53.     char (*Foo())[5];
  54.  
  55. and the tmp in main() should be 
  56.  
  57.     char (*tmp)[5];
  58.  
  59. >> How should i do that?
  60. >> 
  61. >> Any help would be appreciated
  62. >> Anthony
  63.  
  64. Another technique for getting the result back is to pass the array in as an
  65. argument, as is done below (with corrections).
  66.  
  67. >You could pass the pointer to the array to the function.  That way the 
  68. >function itself will fill your array addresses rather than returning 
  69. >redundant data.  You might try:
  70.  
  71. >void foo(char *tmp);
  72.           ^^^^^^^^^ Here, tmp should be declared char (*tmp)[5].
  73. >int main()
  74. >{
  75. >   char tmp[10][5];
  76. >   foo(tmp);
  77. >   return 0;
  78. >}
  79. >void foo(char *tmp)
  80.           ^^^^^^^^^ Here, tmp should be declared char (*tmp)[5].
  81. >{
  82. >/* You can reference tmp within this function as an array of what ever 
  83. > * dimensions you want. */
  84. >/* do something */
  85.  
  86. > return;
  87. >}
  88.  
  89. >HTH
  90.  
  91. >Sean Park
  92. >spark@mc.net
  93. -- 
  94.         Matthew Saltzman
  95.         Clemson University Math Sciences
  96.         mjs@clemson.edu
  97.